//
//  FourViewController.m
//  UI_豆瓣更新版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "FourViewController.h"
#import "LoginViewController.h"
#import "MyActivityViewController.h"
#import "MyMovieViewController.h"
#import "CYDataBaseHandle.h"
@interface FourViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIAlertController *alert;

@end

@implementation FourViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor brownColor];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.arr = [NSMutableArray array];
}

// 登录按钮的点击方法
- (void)buttonAction:(UIBarButtonItem *)item {
    
    LoginViewController *logVc = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:logVc];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
    
    logVc.block = ^(NSString *str) {
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:str style:UIBarButtonItemStylePlain target:self action:@selector(item1Action:)];
        self.navigationItem.rightBarButtonItem = item1;
    };
    
    logVc.block1 = ^(NSString *str1) {
        self.navigationItem.title = str1;
    };
}

// 注销按钮点击方法
- (void)item1Action:(UIBarButtonItem *)item {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:UIAlertControllerStyleAlert];
    //3.在弹出对话框上添加按钮
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
        self.navigationItem.rightBarButtonItem = item1;
        self.navigationItem.title = nil;
    }];

    UIAlertAction *alertaction1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    //4.添加方法
    [alert addAction:alertAction];
    [alert addAction:alertaction1];
    //5.显示视图对话框
    [self presentViewController:alert animated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 12;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
        cell.textLabel.font = [UIFont systemFontOfSize:25];
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";
        cell.textLabel.font = [UIFont systemFontOfSize:25];
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"清除缓存";
        cell.textLabel.font = [UIFont systemFontOfSize:25];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;
}

// cell 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyActivityViewController *MAVc = [[MyActivityViewController alloc] init];
        [self.navigationController pushViewController:MAVc animated:YES];
    }
    if (indexPath.row == 1) {
        MyMovieViewController *mmVc = [[MyMovieViewController alloc] init];
        [self.navigationController pushViewController:mmVc animated:YES];
    }
    if (indexPath.row == 2) {
        self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认清除缓存" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[CYDataBaseHandle sharedDataBaseHandle] deleteAll];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [_alert addAction:action2];
        [_alert addAction:action1];
        [self presentViewController:_alert animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
